安装MySQL

安装 MySQL 依赖

sudo yum update
sudo yum install ncurses-devel cmake gcc gcc-c++ make numactl libaio

创建 MySQL 组及用户

sudo groupadd mysql
sudo useradd -g mysql -r mysql

解压 MySQL 并授予 MySQL 用户权限

tar zxvf mysql-8.0.28-el7-x86_64.tar.gz
mv mysql-8.0.28-el7-x86_64 mysql
chown -R mysql:mysql mysql

添加 mysql 环境变量

cat > /etc/profile.d/mysql.sh << EOF
export MYSQL_HOME=/opt/mysql
export PATH=$MYSQL_HOME/bin:$PATH
EOF

添加 mysql 配置,vim /etc/my.cnf

[mysqld]
sql_mode=STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
datadir=/opt/mysql/data
socket=/tmp/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/home/mysql/mariadb/mariadb.log
pid-file=/home/mysql/mariadb/mariadb.pid
  
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

添加 systemctl 服务

vim /opt/mysql/support-files/mysql.server
//修改两行
basedir=/opt/mysql
datadir=/opt/mysql/data

将 mysql.server 复制到/etc/rc.d/init.d 文件夹中

cp /opt/mysql/support-files/mysql.server /etc/rc.d/init.d/mysql
systemctl daemon-reload

初始化 mysql

su mysql
bash-4.2$ bin/mysqld --initialize
2025-01-13T15:03:21.406627Z 0 [Warning] [MY-010139] [Server] Changed limits: max_open_files: 1024 (requested 8161)
2025-01-13T15:03:21.406664Z 0 [Warning] [MY-010142] [Server] Changed limits: table_open_cache: 431 (requested 4000)
2025-01-13T15:03:21.407768Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2025-01-13T15:03:21.407841Z 0 [Warning] [MY-010915] [Server] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2025-01-13T15:03:21.408045Z 0 [System] [MY-013169] [Server] /opt/mysql/bin/mysqld (mysqld 8.0.28) initializing of server in progress as process 18090
2025-01-13T15:03:21.462971Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2025-01-13T15:03:26.650301Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2025-01-13T15:03:39.990048Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost:sR9.Ffue:El-

记住上面的初始密码,使用 systemctl restart mysql 启动 mysql

systemctl restart mysql
mysql -u root -p
//使用上面的初始密码登录

修改 mysql 用户密码,及授予 mysql 远程访问

ALTER USER 'root'@'localhost' IDENTIFIED BY 'your_new_password';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
CREATE USER 'root'@'%' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;